/*
 * ht1621.c
 *
 *  Created on: 2021年10月12日
 *      Author: wuwenfeng
 */


#include "ht1621.h"
#include "my_code.h"

#define HT1621_addrbit 6

void WriteClockHT1621(void)
{
	HC595_SCK(0);
	HC595_SCK(1);
}

void WriteCommandHT1621(unsigned char FunctonCode)
{
	unsigned char Shift = 0x80;
	unsigned char i;
	HC595_RCK(0);
	HC595_DCK(1);
	WriteClockHT1621();
	HC595_DCK(0);
	WriteClockHT1621();
	HC595_DCK(0);
	WriteClockHT1621();
	for(i = 0; i < 8; i++)
	{
		if(Shift & FunctonCode)
		{HC595_DCK(1);}
		else
		{HC595_DCK(0);}

		 WriteClockHT1621();
		 Shift = Shift >> 1;
	}
	{HC595_DCK(0);}
	WriteClockHT1621();
	HC595_RCK(1);
	HC595_DCK(1);
}



void WritenDataHT1621(unsigned char Addr,unsigned char *Databuf,unsigned char Cnt)
{
	unsigned char i,j;
	unsigned char Shift;
	unsigned char dataval;
	HC595_RCK(0);
	HC595_DCK(1); 		  		//101
	WriteClockHT1621();
	HC595_DCK(0);
	WriteClockHT1621();
	HC595_DCK(1);
	WriteClockHT1621();
	Shift = 0x20;
	for( i = 0; i < HT1621_addrbit; i++)
	{
		if (Addr & Shift)
		{HC595_DCK(1);}
		else
		{HC595_DCK(0);}
		WriteClockHT1621();
		Shift = Shift >> 1;
	}
	for (j = 0; j < Cnt; j++)
	{
		Shift = 0x01;
		dataval=*Databuf++;
		for (i = 0; i < 4; i++)
		{
			if( dataval & Shift)
			{HC595_DCK(1);}
			else
			{HC595_DCK(0);}
			WriteClockHT1621();
			Shift = Shift << 1;
		}
	}
	HC595_RCK(1);
	HC595_DCK(1);
}




void HT1621_Init(void)
{
	HC595_RCK(1);
	HC595_SCK(1);
	HC595_DCK(1);
	WriteCommandHT1621(OSC_ON);
	WriteCommandHT1621(DISP_ON);
	WriteCommandHT1621(COM_1_3__4);
	WriteCommandHT1621(TIMER_DIS);
	WriteCommandHT1621(WDT_DIS);
	WriteCommandHT1621(BUZZ_OFF);
	WriteCommandHT1621(IRQ_DIS);
}


